package leetcode.D600.T543;

import util.TreeNode;

/**
 * @File Info: leetcode -- <Solution>
 * @Author: 18362
 * @Create: 2022-03-15 21:18:18 星期二
 */

class Solution {
    // 一刷
    /*private int res = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        maxDepth(root);
        return res;
    }

    private int maxDepth(TreeNode root) {
        if (root == null)
            return 0;
        int leftDepth = maxDepth(root.left);
        int rightDepth = maxDepth(root.right);
        res = Math.max(res, leftDepth+rightDepth);
        return Math.max(leftDepth, rightDepth) + 1;
    }*/

    // 二刷
    private int res = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        getDepth(root);
        return res;
    }
    public int getDepth(TreeNode root) {
        if (root == null)
            return 0;
        int leftDepth = getDepth(root.left);
        int rightDepth = getDepth(root.right);
        res = Math.max(res, leftDepth + rightDepth);
        return Math.max(leftDepth, rightDepth) + 1;
    }
}